//
//  CalendarViewController.m
//  SAT_NET
//
//  Created by lanou3g on 16/3/8.
//  Copyright © 2016年 郭丹. All rights reserved.
//

#import "CalendarViewController.h"

@interface CalendarViewController ()<JTCalendarDelegate>

@property(nonatomic, strong) JTCalendarManager *calendarManager;
@property(nonatomic, strong) NSDate *todayDate;
@property(nonatomic, strong) NSDate *dateSelected;
@end

@implementation CalendarViewController

- (void)loadView {
    self.calendarV = [[Calendar alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.calendarV;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed: 45 / 255.0
                                                                           green: 128 / 255.0
                                                                            blue: 248 / 255.0
                                                                           alpha:1];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow"] style:(UIBarButtonItemStyleDone) target:self action:@selector(leftAction)];
    
    self.calendarManager = [JTCalendarManager new];
    self.calendarManager.delegate = self;
    
    [self.calendarManager setMenuView:self.calendarV.calendarMenuV];
    [self.calendarManager setContentView:self.calendarV.contentV];
    self.todayDate = [NSDate date];
    [self.calendarManager setDate:[NSDate date]];
    self.dateSelected = [NSDate date];
    [self.calendarManager reload];
}

#pragma mark 设置日历显示
- (void)calendar:(JTCalendarManager *)calendar prepareDayView:(JTCalendarDayView *)dayView {
    //当天
    if ([self.calendarManager.dateHelper date:[NSDate date] isTheSameDayThan:dayView.date]) {
        
        dayView.circleView.hidden = NO;
        dayView.textLabel.text = @"今";
        dayView.circleView.backgroundColor = [UIColor blueColor];
        dayView.textLabel.textColor = [UIColor whiteColor];

//        if ([self.calendarManager.dateHelper date:self.dateSelected isTheSameDayThan:[NSDate date]]) {
//            dayView.circleView.hidden = NO;
//            dayView.textLabel.text = @"今";
//            dayView.circleView.backgroundColor = [UIColor blueColor];
//            dayView.textLabel.textColor = [UIColor whiteColor];
//        } else {
//            dayView.circleView.hidden = NO;
//            dayView.textLabel.text = @"今";
//            dayView.circleView.backgroundColor = [UIColor grayColor];
//            dayView.textLabel.textColor = [UIColor blackColor];
//        }
    }
    // 点击的那天
    else if (self.dateSelected && [self.calendarManager.dateHelper date:self.dateSelected isTheSameDayThan:dayView.date]) {
        dayView.circleView.hidden = NO;
        dayView.circleView.backgroundColor = [UIColor blueColor];
        dayView.textLabel.textColor = [UIColor whiteColor];
    }
    //不属于当月的
    else if (![self.calendarManager.dateHelper date:self.calendarV.contentV.date isTheSameMonthThan:dayView.date]) {
        dayView.circleView.hidden = YES;
        dayView.textLabel.textColor = [UIColor lightGrayColor];
        
    }
    //属于当月但是没有点击
    else {
        dayView.circleView.hidden = YES;
        dayView.textLabel.textColor = [UIColor blackColor];
    }
}

- (void)calendar:(JTCalendarManager *)calendar didTouchDayView:(JTCalendarDayView *)dayView {
    self.dateSelected = dayView.date;
    dayView.circleView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
    [UIView transitionWithView:dayView duration:.3 options:0 animations:^{
        dayView.circleView.transform = CGAffineTransformIdentity;
        [self.calendarManager reload];
    } completion:nil];
    
    if (![self.calendarManager.dateHelper date:self.calendarV.contentV.date isTheSameMonthThan:dayView.date]) {
        if ([self.calendarV.contentV.date compare:dayView.date] == NSOrderedAscending) {
            [self.calendarV.contentV loadNextPageWithAnimation];
        } else {
            [self.calendarV.contentV loadPreviousPageWithAnimation];
        }
    }
    
    
    
}




- (void)leftAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
